package com.besall.allbase.view.activity.chipstoollevel4.protocol.FitnessParams;

import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_FORMAT_CHECK;
import static com.besall.allbase.bluetooth.service.Protocol.ProtocolConstants.KEY_SPO2;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.bes.bessdk.utils.SPHelper;
import  bb.lanxing.R;

public class Spo2For125Hz extends DialogFragment {
        public int v = 0;
        public boolean startOff = false;

    private ConfigCallBack mConfigCallback;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = inflater.inflate(R.layout.config_spo2_125hz, container, false);
        initView(view);
        return view;
    }

    public void initView(View view){

        RadioGroup chooseFormat = (RadioGroup) view.findViewById(R.id.ppg);
        chooseFormat.setOnCheckedChangeListener(FormatCheckChangeListener);
        RadioGroup onOff = (RadioGroup) view.findViewById(R.id.on_off);
        onOff.setOnCheckedChangeListener(OnOffCheckListener);
        view.findViewById(R.id.ok).setOnClickListener(mOkListener);
        view.findViewById(R.id.cancel).setOnClickListener(mCancelListener);
        v = 0;
        startOff = true;
    }

    public boolean saveConfig(){
        if (v == 0){
            if (startOff){
                SPHelper.putPreference(getActivity(), KEY_SPO2, 2);
            } else {
                SPHelper.putPreference(getActivity(), KEY_SPO2, 1);            }
        } else if (v == 1){
            if (startOff){
                SPHelper.putPreference(getActivity(), KEY_SPO2, 130);
            } else {
                SPHelper.putPreference(getActivity(), KEY_SPO2, 129);
            }
        }
        return true;
    }

    private final RadioGroup.OnCheckedChangeListener FormatCheckChangeListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            if(checkedId == R.id.green){
                v = 0;
            }else if(checkedId == R.id.red){
                v = 1;
            }

 /*           switch (checkedId){
                case R.id.green:
                    v = 0;
                    break;
                case R.id.red:
                    v = 1;
                    break;
            }*/
        }
    };

    private final RadioGroup.OnCheckedChangeListener OnOffCheckListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {


            if(checkedId == R.id.start){
                Log.i("TAG", "onCheckedChanged: on" );
                startOff = true;
            }else if(checkedId == R.id.off){
                Log.i("TAG", "onCheckedChanged: off");
                startOff = false;
            }

/*            switch (checkedId){
                case R.id.start:
                    Log.i("TAG", "onCheckedChanged: on" );
                    startOff = true;
                    break;
                case R.id.off:
                    Log.i("TAG", "onCheckedChanged: off");
                    startOff = false;
                    break;
            }*/
        }
    };

    public void setConfigCallback(ConfigCallBack configCallback) {
        mConfigCallback = configCallback;
    }

    private final View.OnClickListener mOkListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (saveConfig()){
                dismissAllowingStateLoss();
                if (mConfigCallback != null){
                    mConfigCallback.onConfigOk();
                }
            }
        }
    };

    private final View.OnClickListener mCancelListener =new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismissAllowingStateLoss();
            if (mConfigCallback != null){
                mConfigCallback.onConfigCancel();
            }
        }
    };


    public interface ConfigCallBack {

        void onConfigOk();

        void onConfigCancel();
    }



}
